package com.marvels.product.param.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dto.McptPaymentSequenceDTO;
import com.marvels.product.param.dto.McptPaymentSequenceItemDTO;
import com.marvels.product.param.service.PaymentSequenceService;

@RestController
@RequestMapping("/product/param/paySequence/service")
public class PaymentSequenceServiceController {

	@Autowired
	private PaymentSequenceService sequenceService;
	
	@RequestMapping("list")
	public MarvelsResultDTO list(@RequestBody Map<String,Object> param){
		
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(param));
		
		PageInfo<McptPaymentSequenceDTO> pageInfo = null;
		
		try {
			pageInfo = sequenceService.getPageInfoByParam(param);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageInfo);
	}
	
	
	@RequestMapping("getKey/{paymentSequenceId}")
	public MarvelsResultDTO edit(@PathVariable Long paymentSequenceId,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(paymentSequenceId));
		McptPaymentSequenceDTO entity = null;
		try {
			 entity = sequenceService.getEntityByPrimaryKey(paymentSequenceId);
			 MarvelsLogUtil.getInstance().info(JSON.toJSONString(entity));
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), entity);
	}
	
	
	@RequestMapping("initInfo")
	public MarvelsResultDTO initInfo(){
		List<McptPaymentSequenceItemDTO> seqList = sequenceService.getInitInfo();
		
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), seqList);
	}
	
	@RequestMapping("save")
	public MarvelsResultDTO save(@RequestBody McptPaymentSequenceDTO dto,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setCreatedBy(userName);
			sequenceService.insert(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dto);
	}
	
	
	@RequestMapping("updateStatus")
	public MarvelsResultDTO updateStatus(@RequestBody McptPaymentSequenceDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			sequenceService.updateStatus(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("还款参数审核异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	@RequestMapping("delete")
	public MarvelsResultDTO delete(@RequestBody McptPaymentSequenceDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			sequenceService.deleteByPrimaryKey(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("还款参数删除异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	@RequestMapping("update")
	public MarvelsResultDTO update(@RequestBody McptPaymentSequenceDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setUpdatedBy(userName);
			sequenceService.updateByPrimaryKey(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}  catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
}
